import time

import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By
from unittest import TestCase
from ddt import ddt, data, unpack


@ddt
class TestFinancing(TestCase):
    change_test_data = [
        ['lisi', '123456', '12345678945213124', '666666'],
        ['lisi', '123456', '12345678945213124', '666667'],
        ['lisi4', '123456', '5555555555555555', '666666'],
        ['lisi5', '123456', '7777777777777777', '666666']
    ]

    term_test_data = [
        ['lisi', '123456', '12345678945213124', '666666'],
        ['lisi', '123456', '12345678945213124', '666667'],
        ['lisi4', '123456', '5555555555555555', '666666'],
        ['lisi5', '123456', '7777777777777777', '666666']
    ]

    fund_test_data = [
        ['lisi', '123456', '12345678945213124', '666666'],
        ['lisi', '123456', '12345678945213124', '666667'],
        ['lisi4', '123456', '1111111111111111', '666666'],
        ['lisi5', '123456', '7777777777777777', '666666']
    ]

    payment_test_data = [
        ['lisi6', '123456', '3333333333333333', '666666'],
        ['lisi7', '123456', '12345678945213124', '666666'],
        ['lisi7', '123456', '5555555555555555', '666666'],
        ['lisi5', '123456', '', '666666']
    ]

    # @data(*change_test_data)
    # @unpack
    # def testChange(self, username, password, cardnum, paypwd):
    #     con = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='finance')
    #     cursor = con.cursor()
    #     sql = f'select balance from bankcard where cardNum = {cardnum}'
    #     cursor.execute(sql)
    #     con.commit()
    #     beforebuy = cursor.fetchall()
    #     beforebalance = beforebuy[0][0]
    #     # option = webdriver.ChromeOptions()
    #     # option.add_argument('--headless')
    #     # 创建谷歌浏览器对象
    #     driver = webdriver.Chrome()
    #
    #     # 窗口最大化
    #     driver.maximize_window()
    #
    #     driver.get("http://localhost:90")
    #
    #     # time.sleep(2)
    #
    #     # 输入用户名
    #     driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
    #
    #     # time.sleep(1)
    #     # 输入密码
    #     driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
    #     # time.sleep(1)
    #
    #     # 点击登陆
    #     driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
    #
    #     time.sleep(1)
    #
    #     # 点击个人理财
    #     driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]').click()
    #     time.sleep(1)
    #     # 点击零钱理财
    #     driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]').click()
    #     time.sleep(1)
    #     # 点击支付宝零钱理财对应的投资按钮
    #     driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
    #     time.sleep(1)
    #     # 输入支付密码
    #     driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys(paypwd)
    #     time.sleep(1)
    #     # 点击确定按钮
    #     driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
    #     time.sleep(1)
    #
    #     # 获取实际结果
    #     cursor.execute(sql)
    #     con.commit()
    #     cursor.close()
    #     con.cursor()
    #     afterbuy = cursor.fetchall()
    #     afterbalance = afterbuy[0][0]
    #
    #     # 退出浏览器
    #     driver.quit()
    #     self.assertEqual(beforebalance - 100, afterbalance)

    # @data(*term_test_data)
    # @unpack
    # def testTerm(self, username, password, cardnum, paypwd):
    #     con = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='finance')
    #     cursor = con.cursor()
    #     sql = f'select balance from bankcard where cardNum = {cardnum}'
    #     cursor.execute(sql)
    #     con.commit()
    #     beforebuy = cursor.fetchall()
    #     beforebalance = beforebuy[0][0]
    #     # option = webdriver.ChromeOptions()
    #     # option.add_argument('--headless')
    #     # 创建谷歌浏览器对象
    #     driver = webdriver.Chrome()
    #
    #     # 窗口最大化
    #     driver.maximize_window()
    #
    #     driver.get("http://localhost:90")
    #
    #     # time.sleep(2)
    #
    #     # 输入用户名
    #     driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
    #
    #     # time.sleep(1)
    #     # 输入密码
    #     driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
    #     # time.sleep(1)
    #
    #     # 点击登陆
    #     driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
    #
    #     time.sleep(1)
    #
    #     # 点击个人理财
    #     driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]').click()
    #     time.sleep(1)
    #     # 点击期限理财
    #     driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]').click()
    #     time.sleep(1)
    #     # 点击支付宝期限理财对应的投资按钮
    #     driver.find_element(By.XPATH,
    #                         '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
    #     time.sleep(1)
    #     # 输入支付密码
    #     driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys(paypwd)
    #     time.sleep(1)
    #     # 点击确定按钮
    #     driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
    #     time.sleep(1)
    #
    #     # 获取实际结果
    #     cursor.execute(sql)
    #     con.commit()
    #     cursor.close()
    #     con.cursor()
    #     afterbuy = cursor.fetchall()
    #     afterbalance = afterbuy[0][0]
    #
    #     # 退出浏览器
    #     driver.quit()
    #     self.assertEqual(beforebalance - 360, afterbalance)

    @data(*payment_test_data)
    @unpack
    def testTerm(self, username, password, cardnum, paypwd):
        con = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='finance')
        cursor = con.cursor()
        cardNumSql = 'select cardNum from bankcard'
        cursor.execute(cardNumSql)
        con.commit()
        cards = cursor.fetchall()
        if (cardnum, ) in cards:
            sql = f'select balance from bankcard where cardNum = {cardnum}'
            cursor.execute(sql)
            con.commit()
            beforebuy = cursor.fetchall()
            beforebalance = beforebuy[0][0]
            # option = webdriver.ChromeOptions()
            # option.add_argument('--headless')
            # 创建谷歌浏览器对象
            driver = webdriver.Chrome()

            # 窗口最大化
            driver.maximize_window()

            driver.get("http://localhost:90")

            # time.sleep(2)

            # 输入用户名
            driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)

            # time.sleep(1)
            # 输入密码
            driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
            # time.sleep(1)

            # 点击登陆
            driver.find_element(By.XPATH, "//button[@id='login_btn']").click()

            time.sleep(1)

            # 点击个人理财
            driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]').click()
            time.sleep(1)
            # 点击基金理财
            driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]').click()
            time.sleep(1)
            # 点击支付宝期限理财对应的投资按钮
            driver.find_element(By.XPATH,
                                '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
            time.sleep(1)
            # 输入支付密码
            driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys(paypwd)
            time.sleep(1)
            # 点击确定按钮
            driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
            time.sleep(1)

            # 获取实际结果
            cursor.execute(sql)
            con.commit()
            cursor.close()
            con.cursor()
            afterbuy = cursor.fetchall()
            afterbalance = afterbuy[0][0]

            # 退出浏览器
            driver.quit()
            self.assertEqual(beforebalance - 108, afterbalance)
        else:
            print('请绑定银行卡！')
            self.assertTrue(False)
